package com.example.hospitallogin.modules.login.bff;

import com.example.hospitallogin.modules.login.dao.entity.DoctorEntity;
import com.example.hospitallogin.modules.login.dao.entity.HospitalMessageEntity;
import com.example.hospitallogin.modules.login.dao.entity.UserLoginExt;
import com.example.hospitallogin.modules.login.dto.*;
import com.example.hospitallogin.modules.login.service.DoctorService;
import com.example.hospitallogin.modules.login.service.HospitalManegerService;
import com.example.hospitallogin.modules.register.dto.LoginRsp;
import com.example.hospitallogin.modules.register.service.LoginService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author qiancheng@cqxyy.net
 * @date 2022/9/15
 */
@Service
public class LoginImpl {
    @Autowired
    private LoginService loginService;

    @Autowired
    private HospitalManegerService hospitalManegerService;

    @Autowired
    private DoctorService doctorService;

    /**
     * 用户登录接口
     * @param req
     * @return
     */
    public Object userLogin(UserLoginReq req) {
        UserLoginExt userMassageEntity = loginService.selectLoginMassage(req);
        LoginRsp loginRsp = new LoginRsp();
        if (userMassageEntity == null){
            return "error";
        }else{
            loginRsp.setName(userMassageEntity.getUserName());
            loginRsp.setSex(userMassageEntity.getSex());
            loginRsp.setType(userMassageEntity.getType());
            loginRsp.setUuid(userMassageEntity.getUuid());
            loginRsp.setAge(getAgeFromIdCard(userMassageEntity.getIdCard()));
        }
        return loginRsp;
    }

    /**
     * 医院登录接口
     * @param req
     * @return
     */
    public Object hospitalLogin(HospitalLoginReq req) {
        Example example = new Example(HospitalMessageEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",req.getPhone()).andEqualTo("password",req.getPassword()).
                andEqualTo("manager",req.getManager()).andEqualTo("idCard",req.getIdCard());
        List<HospitalMessageEntity> list = hospitalManegerService.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return "登录失败,请检查账号登录信息";
        }
        HospitalLoginRsp hospitalLoginRsp = new HospitalLoginRsp();
        BeanUtils.copyProperties(list.get(0),hospitalLoginRsp);
        hospitalLoginRsp.setType(2);
        return hospitalLoginRsp;
    }


    public Object doctorLogin(DoctorLoginReq req) {
        Example example = new Example(DoctorEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if(req.getHospitalUuid()==null || req.getPassword() == null || req.getWorkNumber() == null){
            return "请检查是否有漏填项";
        }
        criteria.andEqualTo("workNumber",req.getWorkNumber());
        criteria.andEqualTo("password",req.getPassword());
        criteria.andEqualTo("hospitalUuid",req.getHospitalUuid());
        List<DoctorEntity> list = doctorService.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return "账号密码错误";
        }
        DoctorLoginRsp doctorLoginRsp = new DoctorLoginRsp();
        BeanUtils.copyProperties(list.get(0),doctorLoginRsp);

        example = new Example(HospitalMessageEntity.class);
        criteria = example.createCriteria();
        criteria.andEqualTo("uuid",req.getHospitalUuid());
        List<HospitalMessageEntity> hospitalMessageEntitys = hospitalManegerService.selectByExample(example);
        if(CollectionUtils.isEmpty(hospitalMessageEntitys)){
            return "医生所属医院不在系统中";
        }
        doctorLoginRsp.setHospitalName(hospitalMessageEntitys.get(0).getHospitalName());
        doctorLoginRsp.setType(4);
        return doctorLoginRsp;
    }


    /**
     * 根据身份证计算当前年龄(精确到日)
     * @param idCard
     * @return
     */
    private Integer getAgeFromIdCard(String idCard) {
        int year =Integer.parseInt(idCard.substring(6,10));
        int mouth =Integer.parseInt(idCard.substring(10,12));
        int day =Integer.parseInt(idCard.substring(12,14));
        SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(System.currentTimeMillis());
        String nowdate = formatter.format(date);
        int nowYear = Integer.parseInt(nowdate.substring(0,4));
        int nowMonth = Integer.parseInt(nowdate.substring(4,6));
        int nowDay = Integer.parseInt(nowdate.substring(6,8));

            int result = nowYear - year;
            if(nowMonth == mouth){
                if(nowDay == day){
                    return result;
                }else if(nowDay < day){
                    return --result;
                }else{
                    return result;
                }
            }else if(nowMonth < mouth){
                return --result;
            }else{
                return result;
            }
    }
}
